# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=postgresql-pg_later
_projname=pg_later
pkgver=0.0.14
pkgrel=1
pkgdesc="PostgreSQL extension to execute SQL now and get the results later"
url="https://github.com/tembo-io/pg_later"
# s390x: fails to build nix crate
# loongarch64: fails to install cargo-pgrx@0.11.3
arch="all !s390x !loongarch64"
license="PostgreSQL"
makedepends="
	cargo
	chrpath
	clang16-libclang
	jq
	openssl-dev
	postgresql
	postgresql-dev
	rustfmt
	"
source="https://github.com/tembo-io/pg_later/archive/v$pkgver/postgresql-pg_later-$pkgver.tar.gz
	remove-tls.patch
	"
builddir="$srcdir/$_projname-$pkgver"
# check: tests tries to write to /usr/lib/...
# net: fetches crates in build phase
options="!check net"

export PGRX_HOME="$srcdir/.pgrx"

prepare() {
	default_prepare

	cargo fetch --target="$CTARGET" --locked
}

build() {
	local pgver="$(pg_config --major-version)"

	# We must install it this way because cargo-pgrx and pgrx dependency
	# versions in the project must be identical. :(
	local pgrx_ver; pgrx_ver="$(cargo metadata --locked --format-version 1 \
		| jq -r '.packages[] | select(.name == "pgrx") | .version')"
	# Use dev profile to speed-up compilation.
	cargo install --locked --profile dev --bins --root target/tools "cargo-pgrx@$pgrx_ver"

	export PATH="$PWD/target/tools/bin:$PATH"

	cargo pgrx init --pg$pgver="$(which pg_config)"

	# TODO: Remove after >0.11.2 is released (https://github.com/pgcentralfoundation/pgrx/pull/1441).
	[ -n "$RUSTFLAGS" ] && export RUSTFLAGS="$(echo $RUSTFLAGS)" || unset RUSTFLAGS

	cargo pgrx package --verbose --out-dir="target/release/dist"

	# Remove RPATH.
	# NOTE: CARGO_PROFILE_RELEASE_RPATH=false doesn't work here.
	chrpath -d target/release/dist/usr/lib/postgresql*/*.so
}

package() {
	depends="postgresql$(pg_config --major-version)"

	mkdir -p "$pkgdir"
	cp -r target/release/dist/* "$pkgdir"/
}

sha512sums="
6685d49411e2c441d53994f7b98e2a0bab84d9d9f047a7d75ee7ff1b2c266f44e4b89ec8b2b4553877e72eceee448fd4dadd37ad8cd15f6e924864aa8b8c9606  postgresql-pg_later-0.0.14.tar.gz
94b8e7e944bb6233d74084b44e5f7c7a2024782a16f5be52718b2ec11cb47b66363d9abf1321879b3cd0c7652ccbfcae83014c34bfb31c77977097b561f7c49c  remove-tls.patch
"
